<?php
set_time_limit(0);
header("Content-type:text/html;charset=utf-8");
include '../nn_logic/nl_common.func.php';
include dirname(dirname(__FILE__)).'/mgtv_v2/models/queue_task_model.php';
$sql="SELECT * FROM `nns_vod_index` WHERE `nns_index` >= '1000' order by nns_name desc";
$dc = nl_get_dc(array(
    'db_policy' => NL_DB_WRITE,
    'cache_policy' => NP_KV_CACHE_TYPE_MEMCACHE
));
$result = nl_query_by_db($sql, $dc->db());
if(!is_array($result) || empty($result))
{
    exit('ok');
}
$arr = null;
foreach ($result as $val)
{
    if(preg_match('/.*\_.*(\d*)/i', $val['nns_name']))
    {
//         $temp = explode('_', $val['nns_name']);
//         $arr[$val['nns_id']] = array(
//             'id'=>$val['nns_id'],
//             'name'=>$val['nns_name'],
//             'nns_index'=>ltrim(preg_replace('/\D/s', '', array_pop($temp)),'0')
//         );
        continue;
    }
    elseif (preg_match('/.*\[(\d*)\]/i', $val['nns_name']))
    {
//         $temp = explode('[', $val['nns_name']);
//         $temp = explode(']', $temp[1]);
//         $arr[$val['nns_id']] = array(
//             'id'=>$val['nns_id'],
//             'name'=>$val['nns_name'],
//             'nns_index'=>ltrim(preg_replace('/\D/s', '', array_shift($temp)),'0')
//         );
        continue;
    }
    elseif (preg_match('/.*1937.*/i', $val['nns_name']))
    {
//         $temp = explode('1937', $val['nns_name']);
//         $arr[$val['nns_id']] = array(
//             'id'=>$val['nns_id'],
//             'name'=>$val['nns_name'],
//             'nns_index'=>ltrim(preg_replace('/\D/s', '', array_pop($temp)),'0')
//         );
        continue;
    }
    elseif (preg_match('/.*1997.*/i', $val['nns_name']))
    {
//         $temp = explode('1997', $val['nns_name']);
//         $arr[$val['nns_id']] = array(
//             'id'=>$val['nns_id'],
//             'name'=>$val['nns_name'],
//             'nns_index'=>ltrim(preg_replace('/\D/s', '', array_pop($temp)),'0')
//         );
        continue;
    }
    else
    {
        $arr[$val['nns_id']] = array(
            'id'=>$val['nns_id'],
            'name'=>$val['nns_name'],
            'nns_index'=>substr($val['nns_name'],0,2),
        );
    }
    
    
//     $result = nl_query_by_db("select count(*) as count from nns_vod_index where nns_vod_id='{$val['nns_id']}'", $dc->db());
//     if(!isset($result[0]['count']) || $result[0]['count'] <1)
//     {
//         continue;
//     }
//     $sql_ex ="update nns_vod set nns_all_index='{$result[0]['count']}' where nns_id='{$val['nns_id']}'";
//     nl_execute_by_db($sql_ex, $dc->db());
//     $queue_task_model = new queue_task_model();
//     $queue_task_model->q_add_task_op_mgtv($val['nns_id'], 'video', 'add','',true,null);
//     unset($queue_task_model);
}
echo json_encode($arr);die;
if(is_array($arr))
{
    foreach ($arr as $exchange_key=>$exchange_val)
    {
        $exchange_val['nns_index'] = (int)$exchange_val['nns_index'];
        if($exchange_val['nns_index']<1)
        {
            continue;
        }
        $exchange_val['nns_index'] --;
        $sql_ex ="update nns_vod_index set nns_index='{$exchange_val['nns_index']}' where nns_id='{$exchange_val['id']}'";
        nl_execute_by_db($sql_ex, $dc->db());
//         $queue_task_model = new queue_task_model();
//         $queue_task_model->q_add_task_op_mgtv($exchange_val['id'], 'index', 'add','',true,null);
//         unset($queue_task_model);
    }
}

echo json_encode($arr);
